package com.ruoyi.web.controller.system;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ModuleTable;
import com.ruoyi.system.service.IModuleTableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模块Controller
 * 
 * @author ruoyi
 * @date 2024-12-06
 */
@RestController
@RequestMapping("/system/module")
public class ModuleTableController extends BaseController
{
    @Autowired
    private IModuleTableService moduleTableService;

    /**
     * 查询模块列表
     */
    @PreAuthorize("@ss.hasPermi('system:module:list')")
    @GetMapping("/list")
    public TableDataInfo list(ModuleTable moduleTable)
    {
        startPage();
        List<ModuleTable> list = moduleTableService.selectModuleTableList(moduleTable);
        return getDataTable(list);
    }

    /**
     * 导出模块列表
     */
    @PreAuthorize("@ss.hasPermi('system:module:export')")
    @Log(title = "模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ModuleTable moduleTable)
    {
        List<ModuleTable> list = moduleTableService.selectModuleTableList(moduleTable);
        ExcelUtil<ModuleTable> util = new ExcelUtil<ModuleTable>(ModuleTable.class);
        util.exportExcel(response, list, "模块数据");
    }

    /**
     * 获取模块详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:module:query')")
    @GetMapping(value = "/{moduleId}")
    public AjaxResult getInfo(@PathVariable("moduleId") Long moduleId)
    {
        return success(moduleTableService.selectModuleTableByModuleId(moduleId));
    }

    /**
     * 新增模块
     */
    @PreAuthorize("@ss.hasPermi('system:module:add')")
    @Log(title = "模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ModuleTable moduleTable)
    {
        return toAjax(moduleTableService.insertModuleTable(moduleTable));
    }

    /**
     * 修改模块
     */
    @PreAuthorize("@ss.hasPermi('system:module:edit')")
    @Log(title = "模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ModuleTable moduleTable)
    {
        return toAjax(moduleTableService.updateModuleTable(moduleTable));
    }

    /**
     * 删除模块
     */
    @PreAuthorize("@ss.hasPermi('system:module:remove')")
    @Log(title = "模块", businessType = BusinessType.DELETE)
	@DeleteMapping("/deleteWithChildren/{moduleIds}")
    public AjaxResult remove(@PathVariable Long[] moduleIds)
    {
        return toAjax(moduleTableService.deleteModuleTableByModuleIdsRecursively(moduleIds));
    }

    /**
     * 新增子模块
     */
    @PreAuthorize("@ss.hasPermi('system:module:add')")
    @Log(title = "模块", businessType = BusinessType.INSERT)
    @PostMapping("/addChild/{parentId}")
    public AjaxResult addChild(@PathVariable("parentId") Long parentId, @RequestBody ModuleTable moduleTable) {
        // 验证父级模块是否存在
        if (parentId == null || !moduleTableService.existsById(parentId)) {
            return AjaxResult.error("指定的父级模块不存在");
        }
        // 设置父级模块ID
        moduleTable.setParentId(parentId);
        // 插入新模块
        int result = moduleTableService.insertModuleTable(moduleTable);
        // 返回操作结果
        return toAjax(result);
    }

    /**
     * 查询模块树形列表
     */
    @PreAuthorize("@ss.hasPermi('system:module:list')")
    @GetMapping("/tree")
    public TableDataInfo tree(ModuleTable moduleTable) {
        startPage();
        List<ModuleTable> list = moduleTableService.selectModuleTreeList(moduleTable);
        return getDataTable(list);
    }



}
